/**
 * This file is part of the CAKIS project.
 * 
 * Copyright (c) 2009-2010 - Florian Arndt, Christian Kerl
 * 
 * @license GNU Lesser General Public License v3 <http://www.opensource.org/licenses/lgpl-3.0.html>
 * @since   13.05.2010
 * @version SVN: $Id: RecipeImportAction.java 146 2010-05-15 12:16:33Z christian-kerl@web.de $
 */
package org.cakis.server.action.impl;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.cakis.server.Names;
import org.cakis.server.model.Recipe;
import org.cakis.server.service.ProductNotFoundException;
import org.cakis.server.service.RecipeImportService;
import org.cakis.server.service.RecipeImportServiceRegistry;
import org.cakis.server.service.RecipeNotFoundException;
import org.cakis.server.service.RecipeService;
import org.cakis.server.service.StockUnitNotFoundException;
import org.hibernate.validator.NotNull;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.framework.Controller;
import org.jboss.seam.international.StatusMessage.Severity;

/**
 * RecipeImportAction
 *
 * @author Christian Kerl
 */
@Name("recipeImport")
@Scope(ScopeType.PAGE)
public class RecipeImportAction extends Controller {
    private static final long serialVersionUID = 1L;

    @In("facesMessages")
    private FacesMessages msg;

    @In(Names.RecipeImportServiceRegistry)
    private RecipeImportServiceRegistry registry;

    @In(Names.RecipeServiceLocal)
    private RecipeService.Local service;

    private String url;
    private Recipe result;

    @Factory
    @DataModel
    public List<RecipeImportService> getImportServices() {
        return registry.getServices();
    }

    @NotNull
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Out(value="recipeImportResult", required=false, scope=ScopeType.CONVERSATION)
    public Recipe getResult() {
        return result;
    }

    @Begin
    public void execute() {
        result = null;

        try {
            URL url = new URL(getUrl());
            result = service.importRecipe(url, true);

            msg.add(Severity.INFO, "Recipe '#0' found!", result.getName());
        } catch (MalformedURLException e) {
            msg.add(Severity.ERROR, "Invalid URL!");
        } catch (RecipeNotFoundException e) {
            msg.add(Severity.ERROR, "Invalid URL!");
        } catch (ProductNotFoundException e) {
            msg.add(Severity.ERROR, "No Product with Name '#0' found!", e.getIdentifier());
        } catch (StockUnitNotFoundException e) {
            msg.add(Severity.ERROR, "No StockUnit with Name or Symbol '#0' found!", e.getUnit());
        }
    }
}
